﻿using RFBR.Filter;
using RFBR.Core;
using RFBR.Core.Commands;
using RFBR.Core.Helpers;
using RFBR.Core.Infrastructure.Models.Structure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RFBR.Controllers
{
    public class KnowledgeBaseController : Controller
    {
        public KnowledgeBaseController()
        {
        }

        //
        // GET: /KnowledgeBase/
        public ActionResult Index()
        {
            IKnowledgeBase kbase = (IKnowledgeBase)Session[SessionKey.KnowledgeBase];
            if (kbase == null)
            {
                kbase = KnowledgeBaseHelper.CreateNewKnowledgeBase();
                Session[SessionKey.KnowledgeBase] = kbase;
            }
            return View();
        }

        //
        // POST: /KnowledgeBase/LoadFrames
        [HttpPost]
        [KnowledgeBaseExist]
        public JsonResult LoadFrames()
        {
            IKnowledgeBase kbase = (IKnowledgeBase)Session[SessionKey.KnowledgeBase];
            return Json(kbase != null ? kbase.ToJSON() : new
            {
                Name = "Knowledge base",
                Frames = new List<dynamic>()
            });
        }

        [HttpPost]
        [KnowledgeBaseExist]
        public void RenameKnowledgeBase(string n)
        {
            IKnowledgeBase kbase = (IKnowledgeBase)Session[SessionKey.KnowledgeBase];
            kbase.Name = n;
            Session[SessionKey.KnowledgeBase] = kbase;
        }

        //
        // POST: /KnowledgeBase/CreateFrame
        [HttpPost]
        [KnowledgeBaseExist]
        public JsonResult CreateFrame(CreateFrameRequest cf)
        {
            IKnowledgeBase kbase = (IKnowledgeBase)Session[SessionKey.KnowledgeBase];
            IFrame frame = kbase.CreateFrame(cf.p, cf.n ?? "New frame");
            Session[SessionKey.KnowledgeBase] = kbase;
            return new JsonResult()
            {
                ContentType = "application/json",
                Data = new CreateFrameResponse()
                {
                    Id = frame.Id,
                    Name = frame.Name
                }
            };
        }

        //
        // POST: /KnowledgeBase/UpdateFrame
        [HttpPost]
        [KnowledgeBaseExist]
        public void UpdateFrame(RFBR.Core.Infrastructure.XmlModels.Frame f)
        {
            IKnowledgeBase kbase = (IKnowledgeBase)Session[SessionKey.KnowledgeBase];
            KnowledgeBaseHelper.UpdateFrame(kbase, f);
        }

        //
        // POST: /KnowledgeBase/GetSlots
        [HttpPost]
        [KnowledgeBaseExist]
        public JsonResult GetSlots(Guid f)
        {
            IKnowledgeBase kbase = (IKnowledgeBase)Session[SessionKey.KnowledgeBase];
            IFrame frame = kbase.GetFrameById(f);
            return Json(frame.Slots.Select(s => new
            {
                s.Name,
                s.Inheritance,
                s.Value.Type,
                Value = KnowledgeBaseHelper.GetEntityJSON(s.Value)
            }));
        }
    }
}